home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 52 / Mac Magazin and MacEasy Magazine CD - Issue 52.iso / Updates / Stata 5.0 Ado-files / ado.sea / newado / stset.ado < prev    next >
Text File  |  1998-11-22  |  10KB  |  491 lines

  1. *! version 5.1.1  26oct1997
  2. program define stset
  3.     version 5.0
  4.     if "`*'"=="" {
  5.         Query check
  6.         exit
  7.     }
  8.     if substr("`1'",1,1) == "," {
  9.         st_is
  10.         local curshow : char _dta[st_show]
  11.         if "`curshow'"=="" {
  12.             local shopt "noShow"
  13.         }
  14.         else     local shopt "Show"
  15.         local options "CLEAR `shopt'"
  16.         parse "`*'" 
  17.         if "`clear'" != "" {
  18.             char _dta[_dta] 
  19.             char _dta[st_t]
  20.             char _dta[st_t0]
  21.             char _dta[st_d]
  22.             char _dta[st_id] 
  23.             char _dta[st_wt]
  24.             char _dta[st_wv]
  25.             char _dta[st_show]
  26.             exit
  27.         }
  28.         if "`show'" != "" {
  29.             if "`curshow'"=="" {
  30.                 char _dta[st_show] "noshow"
  31.             }
  32.             else    char _dta[st_show]
  33.         }
  34.         exit
  35.     }
  36.     local varlist "req ex max(2)"
  37.     local weight "fweight pweight iweight noprefix"
  38.     local options "ID(string) GENT0(string) noShow T0(string) FIrst FORCE"
  39.     parse "`*'"
  40.     parse "`varlist'", parse(" ")
  41.  
  42.     local t "`1'"
  43.  
  44.     if "`2'" != "" {
  45.         local d "`2'"
  46.     }
  47.  
  48.     if "`t0'"!="" {
  49.         unabbrev `t0', max(1)
  50.         local t0 "$S_1"
  51.     }
  52.  
  53.     if "`id'"!="" {
  54.         unabbrev `id', max(1)
  55.         local id "$S_1"
  56.         if "`d'"=="" {
  57.             di in red /*
  58.         */ "you must specify a failure variable when you specify id()"
  59.             exit 198
  60.         }
  61.     }
  62.  
  63.     if "`gent0'"!="" {
  64.         if "`t0'"!="" {
  65.             di in red "may not specify both t0() and gent0()"
  66.             exit 198
  67.         }
  68.         ChkNew gent0() "`gent0'"
  69.         local gent0 "$S_1"
  70.     }
  71.  
  72.  
  73.     if "`weight'"!="" {
  74.         local wtype "`weight'"
  75.         unabbrev `exp', max(1) 
  76.         local wvar "$S_1"
  77.     }
  78.  
  79.     if "`force'"!="" | "`first'"!="" {
  80.         preserve
  81.         local preserv 1
  82.         local origN = _N
  83.     }
  84.  
  85.     if "`force'" != "" {
  86.         local fo_oldN = _N
  87.         qui drop if `t'==.
  88.         if "`d'"!="" {
  89.             qui drop if `d'==.
  90.         }
  91.         if "`t0'"!="" {
  92.             qui drop if `t0'==.
  93.         }
  94.         if "`id'"!="" {
  95.             local idt : type `id'
  96.             if substr("`idt'",1,3)=="str" {
  97.                 qui drop if `id'==""
  98.             }
  99.             else    qui drop if `id'==.
  100.         }
  101.         if "`wvar'"!="" {
  102.             qui drop if `wvar'==.
  103.         }
  104.         local fo_newN = _N
  105.     }
  106.         
  107.     if "`first'"!="" {
  108.         if "`id'"=="" {
  109.             di in red "first requires you also specify id()"
  110.             exit 198
  111.         }
  112.         local fi_oldN = _N
  113.         sort `id' `t'
  114.         if "`d'"=="" {
  115.             qui by `id': keep if _n==1
  116.         }
  117.         else {
  118.             qui by `id': drop if sum(`d'[_n-1]!=0 & `d'[_n-1]!=.)>0
  119.         }
  120.         local fi_newN = _N
  121.     }
  122.  
  123.     Check  "`t0'" `t' "`d'" "`id'" "`wtype'" "`wvar'"
  124.  
  125.     if "`id'"!="" {
  126.         if "`t0'"=="" & "`gent0'"=="" {
  127.             MakeVar "gent0()" t0 time0 etime
  128.             local gent0 "$S_1"
  129.         }
  130.     }
  131.  
  132.     if "`gent0'"!="" {
  133.         Maket0 `gent0' `t' "`id'"
  134.         local t0 "`gent0'"
  135.         label var `t0' "entry time"
  136.     }
  137.  
  138.     char _dta[_dta] 
  139.     if "`show'"=="" {
  140.         char _dta[st_show]
  141.     }
  142.     else    char _dta[st_show] "noshow"
  143.     char _dta[st_t] "`t'"
  144.     char _dta[st_t0] "`t0'"
  145.     char _dta[st_d] "`d'"
  146.     char _dta[st_id] "`id'"
  147.     char _dta[st_wt] "`wtype'"
  148.     char _dta[st_wv] "`wvar'"
  149.     if "`wtype'"!="" {
  150.         char _dta[st_w] "[`wtype'=`wvar']"
  151.     }
  152.     else    char _dta[st_w] 
  153.     char _dta[_dta] "st"
  154.  
  155.     if "`preserv'" != "" {
  156.         restore, not
  157.         if _N != `origN' {
  158.             global S_FN
  159.             global S_FNDATE
  160.         }
  161.     }
  162.     if "`force'" != "" {
  163.         Plural `fo_oldN' - `fo_newN'
  164.         di in gr "note:  " (`fo_oldN'-`fo_newN') /*
  165.         */ " observation$S_1 dropped due to missing"
  166.     }
  167.  
  168.     if "`first'"!="" {
  169.         Plural `fi_oldN' - `fi_newN'
  170.         di in gr /*
  171.         */ "note:  keeping first failures resulted in dropping " /*
  172.             */ (`fi_oldN'-`fi_newN') " observation$S_1"
  173.     }
  174.     global S_1
  175.  
  176.     Query nocheck
  177. end
  178.  
  179. program define Query
  180.     st_is
  181.     local t : char _dta[st_t]
  182.     local t0 : char _dta[st_t0]
  183.     local d : char _dta[st_d]
  184.     local id : char _dta[st_id]
  185.     local w  : char _dta[st_w]
  186.  
  187.     di
  188.  
  189.     if "$S_FN" != "" {
  190.         di in gr _col(4) "data set name:  " in ye "$S_FN"
  191.     }
  192.  
  193.     di _col(15) in gr "id:  " _c
  194.     if "`id'"=="" {
  195.         di in gr "--" _skip(20) "(meaning each record a unique subject)"
  196.     }
  197.     else    di in ye "`id'"
  198.  
  199.     di _col(7) in gr "entry time:  " _c 
  200.     if "`t0'"=="" {
  201.         di in gr "--" _skip(20) "(meaning all entered at time 0)"
  202.     }
  203.     else    di in ye "`t0'"
  204.  
  205.     di _col(8) in gr "exit time:  " in ye "`t'"
  206.  
  207.  
  208.     di _col(3) in gr "failure/censor:  " _c
  209.     if "`d'"=="" {
  210.         di in gr "--" _skip(20) "(meaning all failed)"
  211.     }
  212.     else    di in ye "`d'"
  213.  
  214.  
  215.     if "`w'"!="" {
  216.         di in gr _col(11) "weight:  " in ye "`w'"
  217.     }
  218.     if "`1'"=="check" {
  219.         local wtype : char _dta[st_wt]
  220.         local wvar : char _dta[st_wv]
  221.         di
  222.         Check  "`t0'" `t' "`d'" "`id'" "`wtype'" "`wvar'"
  223.     }
  224. end
  225.  
  226.  
  227. program define Check 
  228.     local t0 "`1'"
  229.     local t  "`2'"
  230.     local d  "`3'"
  231.     local id "`4'"
  232.     local wtype "`5'"
  233.     local wvar "`6'"
  234.  
  235.     IsNorB "`t0'" "entry-time"
  236.     IsN "`t'" "time"
  237.     IsNorB "`d'" "failure/censoring variable"
  238.     
  239.     if "`id'"!="" {
  240.         capture confirm var `id'
  241.         if _rc { 
  242.             di in red "error:  id variable `id' does not exist"
  243.             exit 111
  244.         }
  245.     }
  246.     IsNorB "`wvar'" "weight variable"
  247.  
  248.     if "`wvar'"!=""  {
  249.         local wgt "[`wtype'=`wvar']"
  250.     }
  251.  
  252.     MissChk "id" "`id'" 
  253.     MissChk "exit time" `t'
  254.     MissChk "entry time" `t0'
  255.     MissChk "failure/censoring" `d'
  256.     MissChk "weight" "`wvar'"
  257.  
  258.     capture assert `t'>0
  259.     if _rc {
  260.         di in red "error:  exit time `t' <= 0 in some obs"
  261.         exit 498
  262.     }
  263.  
  264.     if "`t0'" != "" {
  265.         capture assert `t0'>=0
  266.         if _rc { 
  267.             di in red "error:  entry-time `t0' < 0 in some obs"
  268.             exit 498 
  269.         }
  270.         capture assert `t0'<`t'
  271.         if _rc { 
  272.             di in red "error:  entry time `t0' >= exit time `t' in some obs."
  273.             exit 498
  274.         }
  275.     }
  276.  
  277.     if "`wvar'"!="" {
  278.         quietly count if `wvar'>0
  279.         if _result(1)==0 {
  280.             di in red /* 
  281.             */ "error:  all the weights are 0 or negative"
  282.             di in red "        no observations"
  283.             exit 2000
  284.         }
  285.     }
  286.     else {
  287.         if _N==0 {
  288.             di in red "error:  no observations"
  289.             exit 2000
  290.         }
  291.     }
  292.  
  293.     if "`id'"!="" {
  294.         sort `id' `t'
  295.         capture by `id' `t': assert _N==1 
  296.         if _rc { 
  297.             di in red "error:  repeated exit times `t' within `id'"
  298.             exit 498
  299.         }
  300.         if "`t0'" != "" {
  301.             capture by `id': assert `t0'>=`t'[_n-1] if _n>1
  302.             if _rc { 
  303.                 di in red /*
  304. */ "error:  overlapping records within `id'.  That is, some records have" _n /*
  305. */ "        entry-time `t0' > previous record's exit time `t'"
  306.                 exit 498
  307.             }
  308.         }
  309.         if "`wvar'"!="" {
  310.             capture by `id': assert `wvar'==`wvar'[1]
  311.             if _rc {
  312.                 di in red /* 
  313. */ "error:  specified weight is not constant within `id'"
  314.                 exit 498
  315.             }
  316.         }
  317.     }
  318.  
  319.     /* Now the warnings ... */
  320.  
  321.     if "`id'"!="" & "`t0'"!="" {
  322.         capture by `id':  assert `t0'==`t'[_n-1] if _n>1
  323.         if _rc { 
  324.             di in gr /* 
  325. */ "note:  within " in ye "`id'" in gr ", some leave and re-enter the data"
  326.             di in gr "       that is, there are gaps"
  327.         }
  328.     }
  329.  
  330.     if "`id'"!="" {
  331.         sort `id' `t'
  332.         if "`d'"=="" {
  333.             capture by `id': assert _N==1
  334.             if _rc {
  335.                 di in gr /* 
  336. */ "note:  no failure/censoring variable and multiple records per " /*
  337. */ in ye "`id'" _n in gr /* 
  338. */ "       imply multiple failures per subject.
  339.             }
  340.         }
  341.         else {
  342.             capture by `id':  assert `d'==0 if _n!=_N 
  343.             if _rc { 
  344.                 di in gr /* 
  345. */ "note:  within " in ye "`id'" in gr ", some re-enter after failure"
  346.                 tempvar cnt
  347.                 qui by `id': gen `cnt'=sum(`d'!=0)
  348.                 capture by `id': assert `cnt'==1 if _n==_N
  349.                 if _rc { 
  350.                     di in gr /* 
  351. */ "note:  within " in ye "`id'" in gr ", there are multiple failures"
  352.                 }
  353.             }
  354.         }
  355.     }
  356.             
  357.     if "`wvar'"!="" {
  358.         capture assert `wvar'>0 | `wvar'==.
  359.         if _rc {
  360.             capture assert `wvar'>=0 | `wvar'==.
  361.             if _rc {
  362.                 di in gr /*
  363. */ "note:  weight " in ye "`wvar'" in gr " has negative values"
  364.             }
  365.             else    di in gr /*
  366. */ "note:  weight " in ye "`wvar'" in gr " has zero values"
  367.         }
  368.     }
  369. end
  370.  
  371.  
  372.  
  373.  
  374. program define IsN
  375.     capture confirm var `1'
  376.     if _rc {
  377.         di in red "error:  `2' `1' does not exist"
  378.         exit 111
  379.     }
  380.     capture confirm str var 
  381.     if _rc==0 {
  382.         di in red "error:  `2' `1' not numeric"
  383.         exit 109
  384.     }
  385. end
  386.  
  387. program define IsNorB
  388.     if "`1'"=="" { exit }
  389.     IsN `1' "`2'"
  390. end
  391.  
  392. program define MissChk
  393.     local msg "`1'"
  394.     local v "`2'"
  395.     local advice "`3'"
  396.  
  397.     capture confirm var `v'
  398.     if _rc { exit }
  399.     capture confirm string var `v'
  400.  
  401.     if _rc { 
  402.         capture assert `v'!=.
  403.     }
  404.     else    capture assert "`v'"!=""
  405.     if _rc { 
  406.         di in red /*
  407.         */ "error:  `msg' `v' has missing values"
  408.         `advice'
  409.         exit 498
  410.     }
  411. end
  412.  
  413.  
  414. program define MakeVar /* opname <potential-name-list> */
  415.     local option "`1'"
  416.     mac shift
  417.     local list "`*'"
  418.     while "`1'"!="" {
  419.         capture confirm new var `1'
  420.         if _rc==0 {
  421.             global S_1 "`1'"
  422.             exit
  423.         }
  424.         mac shift
  425.     }
  426.     di in red "could not find variable name for `option'"
  427.     di in red "      tried:  `list'"
  428.     di in red "      specify `option' explicitly"
  429.     exit 110
  430. end
  431.  
  432.  
  433. program define Maket0 /* t0 t id */
  434.     local t0 "`1'"
  435.     local t  "`2'"
  436.     local id "`3'"
  437.     di _n in gr "note:  making entry-time variable " in ye "`t0'" _c
  438.     if "`id'"=="" {
  439.         di in gr " = 0"
  440.         qui gen byte `t0' = 0
  441.         exit
  442.     }
  443.     di _n in gr "       (within " in ye "`id'" in gr ", " in ye "`t0'" /*
  444.     */ in gr " will be " in ye "0" in gr " for the 1st observation and the"
  445.     di in gr "       lagged value of exit time " /* 
  446.     */ in ye "`t'" in gr " thereafter)"
  447.     quietly {
  448.         sort `id' `t'
  449.         local ty : type `t'
  450.         by `id': gen `ty' `t0' = cond(_n==1,0,`t'[_n-1])
  451.     }
  452. end
  453.  
  454. program define ChkNew /* optionname varname */
  455.     if "`2'"=="" {
  456.         global S_1
  457.         exit
  458.     }
  459.     local n : word count `1'
  460.     if `n' != 1 { 
  461.         di in red "`1' invalid"
  462.         exit 198
  463.     }
  464.     confirm new var `2'
  465.     global S_1 "`2'"
  466. end
  467.  
  468. program define Plural /* <exp> */
  469.     if (`*') == 1 { 
  470.         global S_1
  471.     }
  472.     else    global S_1 "s"
  473. end
  474.     
  475. exit
  476.  
  477. /*
  478. ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
  479.               Id variable:  id
  480.     Failure-time variable:  t
  481.       Entry-time variable:  t0
  482.   Failure/censor variable:  d (0 means censored)
  483.                    weight:  [fweight=exp]
  484.                
  485.               Id variable:  (none -- meaning each record a unique subject)
  486.     Failure-time variable:  t
  487.       Entry-time variable:  (none -- meaning all entered at time 0)
  488.   Failure/censor variable:  (none -- meaning all assumed to have failed)
  489.                    weight:  [fweight=exp]
  490. */
  491.